package com.tgy.apollo.config;

import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigService;
import com.ctrip.framework.apollo.model.ConfigChange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/***
 * @ClassName: ApolloConfig
 * @Description:
 * @Auther: 送你一块二向箔
 * @Date: 15:09 2022/4/15
 * @version : V1.0
 */
@Configuration
public class ApolloConfig {

    /**
     * @Param  监听获取默认的配置
     * @Description: TODO
     * @auther: TangGuoyi
     * @date: 15:21 2022/4/15
     * @return:
     */
    @Bean
    public void config() {
        // config instance is singleton for each namespace and is never null
        Config config = ConfigService.getAppConfig();
        config.addChangeListener(changeEvent -> {
            System.out.println("Changes for namespace " + changeEvent.getNamespace());
            for (String key : changeEvent.changedKeys()) {
                ConfigChange change = changeEvent.getChange(key);
                System.out.println(String.format("Found change - key: %s, oldValue: %s, newValue: %s, changeType: %s", change.getPropertyName(), change.getOldValue(), change.getNewValue(), change.getChangeType()));
            }
        });
    }

    /**
     * @Param 监听获取namespace变化
     * @Description: TODO
     * @auther: TangGuoyi
     * @date: 15:24 2022/4/15
     * @return:
     */
    @Bean
    public void config1(){
        Config config = ConfigService.getConfig("FX.Hermes.Producer");
        config.addChangeListener(changeEvent->{
                System.out.println("Changes for namespace " + changeEvent.getNamespace());
                for (String key : changeEvent.changedKeys()) {
                    ConfigChange change = changeEvent.getChange(key);
                    System.out.println(String.format(
                            "Found change - key: %s, oldValue: %s, newValue: %s, changeType: %s",
                            change.getPropertyName(), change.getOldValue(),
                            change.getNewValue(), change.getChangeType()));
                }
            });
    }
}
